package Servlet.Test;
/*
@ClassName:     MyServlet01
@Author:        YangXu
@Need:          
@Date:          2022/4/26
@Time:          2022-04-26 11:45
*/

//


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

@WebServlet("/t1")
public class MyServlet01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("接收到了get的请求方式");


        //1、获取到请求行
        String method = req.getMethod();
        System.out.println("method:"+method);//得到的是GET
        //2、获取到虚拟目录的路径
        String contextPath = req.getContextPath();
        System.out.println(contextPath);
        //3、 获取 Servlet的路径
        String servletPath = req.getServletPath();
        System.out.println("Servlet的地址是:"+servletPath);
        resp.getWriter().write("\nServlet的地址是:"+servletPath);
        //4、获取get方式请求参数
        //t1?name=123
        String queryString = req.getQueryString();
        resp.getWriter().write("\n请求Url的参数"+queryString);
        System.out.println(queryString);

        // 5、获取请求的Url
        // 得到请求的url地址
        String requestURI = req.getRequestURI();
        resp.getWriter().write("\n得到请求的url是:"+requestURI);
        System.out.println(requestURI);

        // 6、获取协议的版本
        String protocol = req.getProtocol();
        resp.getWriter().write("\n协议："+protocol);
        System.out.println(protocol);

        // 7、获取到客户机的IP地址
        String remoteAddr = req.getRemoteAddr();
        System.out.println(remoteAddr);

        // ???
/*        String remoteUser = req.getRemoteUser();
        System.out.println(remoteUser);*/

        //  得到请求的路径
        /*String requestURI1 = req.getRequestURI();
        System.out.println(requestURI1);*/

        // 8、请求头
        String header = req.getHeader("User-Agent");
        System.out.println("请求头UA是:\t"+header);

        if (header.contains("Chrome")){
            System.out.println("来自谷歌浏览器");
        }
        //遍历所有请求头
        Enumeration<String> headerNames = req.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String s = headerNames.nextElement();
            String header1 = req.getHeader(s);
            System.out.println(s+":"+header1);
        }
        String referer = req.getHeader("referer");
        if (referer != null) {
            if (referer.contains("/Java_Servlet_war_exploded/")) {
                System.out.println("可以正常播放电影");
            }else {
                System.out.println("不是来自本站链接，不可以播放");
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("接收到了post请求");
        //获取到请求行
        String method = req.getMethod();
        System.out.println("method:"+method);//得到的是post
        String contextPath = req.getContextPath();
        System.out.println(contextPath);
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getMethod();
        System.out.println("method:"+method);//得到的是Put
    }
}
/*

 */